TidyTuesday

Personal aim

My today’s goal is to have a look at plotly animations (and maybe even use them) and the r paletteR package to create pallettes extracted from an image of choice.

Load the weekly Data

Download the weekly data and make available in the tt object.

tt <- tt_load("2021-06-08")
## 
##  Downloading file 1 of 2: `stocked.csv`
##  Downloading file 2 of 2: `fishing.csv`
stocked<- tt$stocked
fishing<- tt$fishing

Readme

Take a look at the readme for the weekly data to get insight on the dataset. This includes a data dictionary, source, and a link to an article on the data.

tt

Glimpse Data

Take an initial look at the format of the data available.

stocked %>% 
  summary()
##       SID                YEAR          MONTH             DAY       
##  Min.   :       1   Min.   :1950   Min.   : 1.000   Min.   : 0.00  
##  1st Qu.:   15377   1st Qu.:1989   1st Qu.: 4.000   1st Qu.: 8.00  
##  Median :   31096   Median :1999   Median : 5.000   Median :16.00  
##  Mean   : 8522639   Mean   :1998   Mean   : 5.291   Mean   :15.65  
##  3rd Qu.:20132395   3rd Qu.:2008   3rd Qu.: 6.000   3rd Qu.:23.00  
##  Max.   :20198457   Max.   :2018   Max.   :12.000   Max.   :31.00  
##                                    NA's   :11414    NA's   :13233  
##      LAKE            STATE_PROV            SITE             ST_SITE         
##  Length:56232       Length:56232       Length:56232       Length:56232      
##  Class :character   Class :character   Class :character   Class :character  
##  Mode  :character   Mode  :character   Mode  :character   Mode  :character  
##                                                                             
##                                                                             
##                                                                             
##                                                                             
##  LATITUDE       LONGITUDE            GRID       STAT_DIST        
##  Mode:logical   Mode :logical   Min.   :   0   Length:56232      
##  NA's:56232     FALSE:2         1st Qu.: 607   Class :character  
##                 NA's :56230     Median : 905   Mode  :character  
##                                 Mean   :1057                     
##                                 3rd Qu.:1410                     
##                                 Max.   :2806                     
##                                 NA's   :3089                     
##    LS_MGMT            SPECIES             STRAIN            NO_STOCKED      
##  Length:56232       Length:56232       Length:56232       Min.   :       0  
##  Class :character   Class :character   Class :character   1st Qu.:    7213  
##  Mode  :character   Mode  :character   Mode  :character   Median :   15000  
##                                                           Mean   :   35368  
##                                                           3rd Qu.:   34600  
##                                                           Max.   :16586200  
##                                                           NA's   :7         
##    YEAR_CLASS       STAGE              AGEMONTH         MARK          
##  Min.   :   0    Length:56232       Min.   :  0.0   Length:56232      
##  1st Qu.:1995    Class :character   1st Qu.: 10.0   Class :character  
##  Median :2003    Mode  :character   Median : 15.0   Mode  :character  
##  Mean   :2001                       Mean   : 13.6                     
##  3rd Qu.:2010                       3rd Qu.: 16.0                     
##  Max.   :2018                       Max.   :180.0                     
##  NA's   :17384                      NA's   :29511                     
##     MARK_EFF       TAG_NO           TAG_RET           LENGTH       
##  Min.   :  0.00   Mode:logical   Min.   :  0.00   Min.   :    0.0  
##  1st Qu.: 90.20   NA's:56232     1st Qu.: 95.20   1st Qu.:  129.0  
##  Median : 98.00                  Median : 97.87   Median :  161.4  
##  Mean   : 79.16                  Mean   : 95.15   Mean   :  160.6  
##  3rd Qu.: 99.40                  3rd Qu.: 99.00   3rd Qu.:  185.5  
##  Max.   :100.00                  Max.   :100.00   Max.   :13780.0  
##  NA's   :50721                   NA's   :53587    NA's   :30652    
##      WEIGHT          CONDITION       LOT_CODE          STOCK_METH       
##  Min.   :    0.0   Min.   :0.000   Length:56232       Length:56232      
##  1st Qu.:  136.1   1st Qu.:0.000   Class :character   Class :character  
##  Median :  453.6   Median :0.000   Mode  :character   Mode  :character  
##  Mean   :  678.6   Mean   :0.835                                        
##  3rd Qu.:  862.0   3rd Qu.:1.000                                        
##  Max.   :50268.0   Max.   :7.000                                        
##  NA's   :13445     NA's   :16868                                        
##     AGENCY            VALIDATION        NOTES          
##  Length:56232       Min.   : 0.000   Length:56232      
##  Class :character   1st Qu.: 2.000   Class :character  
##  Mode  :character   Median : 5.000   Mode  :character  
##                     Mean   : 5.798                     
##                     3rd Qu.: 9.000                     
##                     Max.   :10.000                     
##                     NA's   :10281
fishing %>%
   summary()
##       year          lake             species           grand_total     
##  Min.   :1867   Length:65706       Length:65706       Min.   :    0.0  
##  1st Qu.:1922   Class :character   Class :character   1st Qu.:   10.0  
##  Median :1958   Mode  :character   Mode  :character   Median :  107.6  
##  Mean   :1954                                         Mean   : 1411.9  
##  3rd Qu.:1988                                         3rd Qu.:  964.5  
##  Max.   :2015                                         Max.   :48821.0  
##                                                       NA's   :31767    
##    comments            region              values     
##  Length:65706       Length:65706       Min.   :  -31  
##  Class :character   Class :character   1st Qu.:    0  
##  Mode  :character   Mode  :character   Median :   17  
##                                        Mean   :  514  
##                                        3rd Qu.:  217  
##                                        Max.   :48405  
##                                        NA's   :21916
# quite few missing values in both datasets

Wrangle

Explore the data and process it into a nice format for plotting! Access each dataset by name by using a dollarsign after the tt object and then the name of the data set.

# remove missing data

# fishing %>%
  # filter(year==1991, lake=='Erie', species=='American Eel') # checking if there are any comments

fishing <-fishing %>%
   select(year, lake,species, grand_total) %>%
   filter(year != is.na(year) & lake != is.na(lake) & species != is.na(species) & grand_total != is.na(grand_total))%>% unique()

top_species<- fishing %>% select(species, grand_total,year)%>%
   group_by(year)%>%
   arrange(desc(grand_total), .by_group = TRUE)%>%select(species) %>% filter(year ==1879)


my_fish <-unique(top_species$species)

   
 summary_f <-  fishing %>%
   group_by(year)%>%
   summarise(mean=mean(grand_total))%>%
      kable()%>%
      kable_styling()

Visualize

Using your processed dataset, create your unique visualization.

my_data<- fishing %>%
   filter(species == 'Carp' | species == 'Lake Trout' | species=='Lake Whitefish') 

plot<-ggplot(data= my_data, aes(x=year, y= grand_total, color = species))+
   geom_point(size=0.3)+
   stat_smooth(size=1.7, se = FALSE)+
   scale_color_paletteer_d('awtools::a_palette')+
   ylab(label = "fish total")+
   xlab(label='year')+
   labs(title = 'Fish in the Great Lakes')+
   bbc_style()

Creating a palette from an image

  • using paletteR package
scream_palette<-create_palette(image_path ="scream.jpg",
               number_of_colors =5,
               type_of_variable = "categorical")

plot_scream<-ggplot(data= my_data, aes(x=year, y= grand_total, color = species))+
   geom_point(size=0.3)+
   stat_smooth(size=1.7, se = FALSE)+
   scale_color_manual(values = c(scream_palette[1], scream_palette[4], scream_palette[5] ))+
   ylab(label = "fish total")+
   xlab(label='year')+
   labs(title = 'Fish in the Great Lakes')+
   bbc_style()

plot_scream

pretty cool

fig <- ggplotly(plot)

fig
steps <- list(
  list(args = list("marker.year", "red"), 
                    label = "1880", 
                    method = "restyle", 
                    value = "1"
                    ),
  list(args = list("marker.year", "green"), 
                    label = "1920", 
                    method = "restyle", 
                    value = "2"
                    ),
  list(args = list("marker.year", "blue"), 
                    label = "1960", 
                    method = "restyle", 
                    value = "3"
                    ),
  list(args = list("marker.year", "yellow"), 
                    label = "2000", 
                    method = "restyle", 
                    value = "4"
                    )
  )

anim <- fig %>%
   layout(title = "Fish",
         sliders = list(
           list(
             active = 1, 
             currentvalue = list(prefix = "Year: "), 
             pad = list(t = 60), 
             steps = steps))) 

anim

Save Image

Save your image for sharing. Be sure to use the #TidyTuesday hashtag in your post on twitter!

# This will save your most recent plot
ggsave(plot=plot,
  filename = "My TidyTuesday Plot.png",
  device = "png")
ggsave(plot=plot_scream,
  filename = "My TidyTuesday Plot with paletter.png",
  device = "png")